using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using Qreed.Windows.Forms;
using Qreed.Windows.Forms.Hook;
using System.Windows.Forms;

namespace Qreed.Windows
{
    public static class User32
    {
        #region Structs
        
        [StructLayout(LayoutKind.Sequential)]
        public class POINT
        {
            public int x;
            public int y;
        }

        [StructLayout(LayoutKind.Sequential)]
        public class RECT
        {
            public int left;
            public int top;
            public int right;
            public int bottom;
        }

        #endregion

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
        public static extern IntPtr GetFocus();

        /// <summary>
        /// Gets the focus control.
        /// </summary>
        /// <returns></returns>
        public static Control GetFocusControl()
        {
            Control focusControl = null;
            IntPtr focusHandle = GetFocus();
            if (focusHandle != IntPtr.Zero)
                focusControl = Control.FromHandle(focusHandle);

            return focusControl;
        } 

        /// <summary>
        /// Provides access to function required to delete handle. This method is used internally
        /// and is not required to be called separately.
        /// </summary>
        /// <param name="hIcon">Pointer to icon handle.</param>
        /// <returns>N/A</returns>
        [DllImport("user32.dll")]
        public static extern int DestroyIcon(IntPtr hIcon);

        [DllImport("user32.dll")]
        public static extern Int32 SendMessage(IntPtr pWnd, UInt32 uMsg, UInt32 wParam, UInt32 lParam);

        #region Hooking
        
        public enum HookAction : int
        {
            HCBT_MOVESIZE = 0,
            HCBT_MINMAX = 1,
            HCBT_QS = 2,
            HCBT_CREATEWND = 3,
            HCBT_DESTROYWND = 4,
            HCBT_ACTIVATE = 5,
            HCBT_CLICKSKIPPED = 6,
            HCBT_KEYSKIPPED = 7,
            HCBT_SYSCOMMAND = 8,
            HCBT_SETFOCUS = 9
        }

        public enum HookType : int
        {
            WH_JOURNALRECORD = 0,
            WH_JOURNALPLAYBACK = 1,
            WH_KEYBOARD = 2,
            WH_GETMESSAGE = 3,
            WH_CALLWNDPROC = 4,
            WH_CBT = 5,
            WH_SYSMSGFILTER = 6,
            WH_MOUSE = 7,
            WH_HARDWARE = 8,
            WH_DEBUG = 9,
            WH_SHELL = 10,
            WH_FOREGROUNDIDLE = 11,
            WH_CALLWNDPROCRET = 12,
            WH_KEYBOARD_LL = 13,
            WH_MOUSE_LL = 14
        }

        [DllImport("user32.dll", SetLastError=true)]
        public static extern IntPtr SetWindowsHookEx(HookType type,
                                                     HookProc proc,
                                                     IntPtr hInstance,
                                                     int threadID);
        
        [DllImport("user32.dll")]
        public static extern int UnhookWindowsHookEx(IntPtr hhook);
        
        [DllImport("user32.dll")]
        public static extern int CallNextHookEx(IntPtr hhook, int code, IntPtr wParam, IntPtr lParam);

        #endregion

        [DllImport("user32.dll")]
        public static extern IntPtr CreateWindowEx(uint dwExStyle,			// extended window style
                                                      string lpClassName,		// registered class name
                                                      string lpWindowName,	// window name
                                                      uint dwStyle,			// window style
                                                      int x,					// horizontal position of window
                                                      int y,					// vertical position of window
                                                      int nWidth,				// window width
                                                      int nHeight,			// window height
                                                      IntPtr hWndParent,      // handle to parent or owner window
                                                      IntPtr hMenu,			// menu handle or child identifier
                                                      IntPtr hInstance,		// handle to application instance
                                                      IntPtr lpParam			// window-creation data
                                                      );

        [DllImport("user32.dll")]
        public static extern int MessageBox(IntPtr hwnd, string text, string caption, int options);

        [DllImport("user32.dll")]
        public static extern void DestroyWindow(IntPtr hwnd);

        [DllImport("user32.dll")]
        public static extern IntPtr GetDlgItem(IntPtr hwnd, int id);

        [DllImport("user32.dll")]
        public static extern int GetWindowRect(IntPtr hwnd, RECT rc);

        [DllImport("user32.dll")]
        public static extern int GetClientRect(IntPtr hwnd, RECT rc);

        [DllImport("user32.dll")]
        public static extern void MoveWindow(IntPtr hwnd, int x, int y, int nWidth, int nHeight, bool bRepaint);

        [DllImport("user32.dll")]
        public static extern int ScreenToClient(IntPtr hwnd, POINT pt);

        [DllImport("user32.dll")]
        public static extern int GetClassName(IntPtr hwnd, StringBuilder lpClassName, int nMaxCount);

        [DllImport("user32.dll")]
        public static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int nMaxCount);
    }
}
